//
//  AccountViewController.m
//  uplife
//
//  Created by ZhangCheng on 2018/12/24.
//  Copyright © 2018年 ZhangCheng. All rights reserved.
//

#import "AccountViewController.h"

@interface AccountViewController ()
@property(nonatomic,weak)IBOutlet UIView *borderView1;
@property(nonatomic,weak)IBOutlet UIView *borderView2;
@property(nonatomic,weak)IBOutlet UIView *borderView3;

@property(nonatomic,weak)IBOutlet UIView *loginView;
@property(nonatomic,weak)IBOutlet UIView *forgetPWDView;
@property(nonatomic,weak)IBOutlet UIButton *closePWDViewBtn;

@property(nonatomic,weak)IBOutlet UITextField *usernameTF;
@property(nonatomic,weak)IBOutlet UITextField *pwdTF;

@property(nonatomic,weak)IBOutlet UITextField *emailTF;
@property(nonatomic,weak)IBOutlet UIButton *checkBtn;
@end

@implementation AccountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.borderView1.layer setBorderColor:[UIColor colorWithRed:203.0f/255.0f green:172.0f/255.0f blue:116.0f/255.0f alpha:1.0f].CGColor];
    [self.borderView2.layer setBorderColor:[UIColor colorWithRed:203.0f/255.0f green:172.0f/255.0f blue:116.0f/255.0f alpha:1.0f].CGColor];
    [self.borderView3.layer setBorderColor:[UIColor colorWithRed:203.0f/255.0f green:172.0f/255.0f blue:116.0f/255.0f alpha:1.0f].CGColor];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

-(IBAction)checkAction{
    self.checkBtn.selected = !self.checkBtn.selected;
}

-(IBAction)showPWDViewAction{
    [self showPWDView:YES];
}

-(IBAction)closePWDViewAction{
    [self showPWDView:NO];
}

-(void)showPWDView:(BOOL)PWDViewShouldShow{
    self.loginView.hidden = PWDViewShouldShow;
    self.forgetPWDView.hidden = self.closePWDViewBtn.hidden = !PWDViewShouldShow;
}

-(BOOL)canSendRequestForLogin{
    BOOL result = YES;
    NSString *username = [self.usernameTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *psw = [self.pwdTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if (username.length==0) {
        [SVProgressHUD showErrorWithStatus:@"请输入用户名"];
        result = NO;
        return result;

    }
    if (psw.length==0) {
        [SVProgressHUD showErrorWithStatus:@"请输入密码"];
        result = NO;
        return result;

    }
    if (self.checkBtn.selected==NO) {
        [SVProgressHUD showErrorWithStatus:@"请阅读并同意用户使用条款"];
        result = NO;
        return result;

    }
    return result;
}

-(BOOL)canSendRequestForResetPWD{
    BOOL result = YES;
    NSString *username = [self.emailTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if (username.length==0) {
        [SVProgressHUD showErrorWithStatus:@"请输入邮箱"];
        result = NO;
        return result;
    }
    if (![[ZCTool sharedInstance]isValidateEmail:username]) {
        [SVProgressHUD showErrorWithStatus:@"邮箱格式不正确"];
        result = NO;
        return result;
    }
    return result;
}

-(IBAction)loginAction{
    if ([self canSendRequestForLogin]) {
        NSString *username = [self.usernameTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        NSString *psw = [self.pwdTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        
        //    BOOL isPhone = [[ZCTool sharedInstance]validateMobile:username];
        
        NSString *urlString = [NSString stringWithFormat:@"%@/user/login.do",JSXF_HOST];
        NSDictionary *params = @{
                                 @"username":username,
                                 @"password":psw
                                 };
        
        [SVProgressHUD show];
        
        [ZCWebServiceHelper requestWithMethod:POST andUrlStr:urlString andParameters:params andFinished:^(id response, NSURLSessionDataTask *dataTask) {
            [SVProgressHUD showSuccessWithStatus:response[@"message"]];
            [self loginFinished:response];
        }];
    }
}
-(void)loginFinished:(NSDictionary *)dic{
    [[ZCTool sharedInstance]login:dic[@"data"]];
    [[ZCTool sharedInstance]saveTokenString:dic[@"data"][@"token"]];
    [UIApplication sharedApplication].keyWindow.rootViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"TabBarViewController"];
}

-(IBAction)resetPasswordAction{
    if ([self canSendRequestForResetPWD]) {
        NSString *username = [self.emailTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        
        //    BOOL isPhone = [[ZCTool sharedInstance]validateMobile:username];
        
        NSString *urlString = [NSString stringWithFormat:@"%@/user/findPwd.do",JSXF_HOST];
        NSDictionary *params = @{
                                 @"email":username
                                 };
        
        [SVProgressHUD show];
        
        [ZCWebServiceHelper requestWithMethod:POST andUrlStr:urlString andParameters:params andFinished:^(id response, NSURLSessionDataTask *dataTask) {
            [SVProgressHUD showSuccessWithStatus:response[@"message"]];
            [self closePWDViewAction];
        }];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
